package com.pl.itsense.gwt.clientzone.ui;


import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.pl.itsense.gwt.clientzone.client.ClientZoneServiceAsync;
import com.pl.itsense.gwt.clientzone.client.Clientzone;
import com.pl.itsense.gwt.clientzone.model.MemberDTO;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler; 
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class LoginPanel extends Window{
	
	DynamicForm form = new DynamicForm();
	DynamicForm form2 = new DynamicForm();

	static LoginPanel instance = null;
	IButton loginButton = new IButton("Zaloguj");
	
	public static void hideInstance(){
		if (instance != null){
			instance.setVisible(false);
			instance.destroy();
			instance = null;
		}
	}

	public static void showInstance(){
		if (instance != null){
			instance.setVisible(true);
		}else{
			RootPanel.get().add( new LoginPanel());
			instance.setVisible(true);
		}
	}

	
	
	public LoginPanel() {
		instance = this;
		init();
	}
	
	
	private void init(){
		setWidth(360);  
		setHeight(190);
		setVisible(true);
		setShowCloseButton(false);
		setTitle("IT Sense - strefa klienta");  
		setShowMinimizeButton(false);
		setIsModal(true);  
		setShowModalMask(true);  
		centerInPage();  
		setCanDragReposition(false);
		   
		
		final Img keyImage = new Img("/images/key-icon.png", 48, 48);
		
		TextItem login = new TextItem();  
		login.setName("login");
		login.setTitle("Login");
		login.setRequired(true);
		
		PasswordItem password = new PasswordItem();  
		password.setName("password");
		password.setTitle("Hasło");  
		password.setRequired(true);  
		

		LinkItem newUserItem = new LinkItem();
		newUserItem.setName("newUserItem");
		newUserItem.setTitle("Nowy klient ?");
		newUserItem.setLinkTitle("Utwórz konto ...");
		newUserItem.setWrapTitle(false);
		newUserItem.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
			@Override
			public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
				Clientzone.setApplicationState(1);
			}
		});
		
		
		
		LinkItem forgetPassword = new LinkItem();
		forgetPassword.setName("forgetPassword");
		forgetPassword.setTitle("Zapomniałeś hasło ?");
		forgetPassword.setLinkTitle("Przypomnij mi ...");
		forgetPassword.setWrapTitle(false);
		forgetPassword.setWrap(false);
		forgetPassword.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
			@Override
			public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
				
			}
		});		
		
		form2.setFields(newUserItem,forgetPassword);
		form.setFields(login,password);
		
	
		loginButton.addClickHandler(new ClickHandler() {  
			public void onClick(ClickEvent event) {
				
				if( form.validate() == false) return;
				
				SC.showPrompt("Proszę czekać ...");
				Clientzone.clientZoneService.login(form.getValueAsString("login"), form.getValueAsString("password"), new AsyncCallback<MemberDTO>() {
					
					public void onSuccess(MemberDTO result) {
						SC.clearPrompt();
						Clientzone.memberDTO = null;
						if (result == null){
							SC.warn("Podana login lub hasło nie są poprawne.");
							form.setValue("login", "");
							form.setValue("password","");
						}else{
							if (result.getId() == -1){
								SC.warn("Konto nie zostało aktywowane. Sprawdź swoją pocztę i dokończ proces rejestracji.");
								form.setValue("login", "");
								form.setValue("password","");
								
							}else{
								Clientzone.memberDTO = result;
								Clientzone.setApplicationState(2);
							}
						}

					}
					
					public void onFailure(Throwable caught) {
						SC.clearPrompt();
					}
				});
			}
		});  

		form.setTop(20);
		form.setLeft(40);
		loginButton.setTop(80);
		loginButton.setLeft(130);
		form2.setWidth(200);
		form2.setTop(110);
		form2.setLeft(50);
		keyImage.setTop(20);
		keyImage.setLeft(260);
		
		VLayout vLayout = new VLayout();
		vLayout.addChild(form);
		vLayout.addChild(loginButton);
		vLayout.addChild(form2);
		vLayout.addChild(keyImage);
		
		addItem(vLayout);
		
	
	}
}
